import { execute } from "../../database/connector";

import { RoleQueries } from "../queries/role.query";
import { IRole } from "../../models/role.model";

/**
 * gets active roles
 */
export const getRoles = async () => {
  return execute<IRole[]>(RoleQueries.GetRoles, []);
};

/**
 * gets a role based on id provided
 */
export const getRoleById = async (id: IRole['id']) => {
  return execute<IRole[]>(RoleQueries.GetRoleById, [id]);
};

/**
 * gets a role based on id provided
 */
export const getRolesByIds = async (idList: IRole['id'][]) => {
  return execute<IRole[]>(RoleQueries.GetRolesByIds, [[idList]]);
};

/**
 * adds a new active role record
 */
export const insertRole = async (role: IRole) => {
  const result = await execute<{ affectedRows: number }>(RoleQueries.AddRole, [
    [Object.values(role)]
  ]);
  return result.affectedRows > 0;
};

/**
 * updates role information based on the id provided
 */
export const updateRole = async (role: IRole) => {
  const result = await execute<{ affectedRows: number }>(RoleQueries.UpdateRoleById, [role, role.id]);
  return result.affectedRows > 0;
};

/**
 * updates role information based on the id provided
 */
 export const deleteRole = async (id: IRole['id']) => {
  const result = await execute<{ affectedRows: number }>(RoleQueries.DeleteRoleById, [
    id
  ]);
  return result.affectedRows > 0;
};